<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/
      思路：https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/solution/dfssuan-fa-by-user3502je/
    */
    function exist(board, word) {
      let row = board.length
      let col = board[0].length

      function dfs(i, j, board, word, index) {
        if (i < 0 || i >= row || j < 0 || j >= col || board[i][j] !== word[index])  return false
        if (index === word.length - 1)  return true

        let temp = board[i][j]
        board[i][j] = ''
        let res = dfs(i + 1, j, board, word, index + 1) || dfs(i - 1, j, board, word, index + 1) ||
                  dfs(i, j + 1, board, word, index + 1) || dfs(i, j - 1, board, word, index + 1)
        board[i][j] = temp
        return res
      }

      for (let i = 0; i < row; i++) {
        for (let j = 0; j < col; j++) {
          if (dfs(i, j, board, word, 0))  return true
        }
      }
      return false
    }
  </script>
</body>
</html>